﻿Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data.SqlTypes
Public Class Connection
    Public Function ObjectGetAll_Query_SAP(ByVal QueryString As String) As DataSet
        Try
            Dim sConnSAP As New SqlConnection
            Dim sCon As String = ""
            Dim SQLType As String = ""
            Dim MyArr As Array
            sCon = System.Configuration.ConfigurationSettings.AppSettings.Get("SAPConnect")
            MyArr = sCon.Split(";")
            sCon = "server= " + MyArr(3).ToString() + ";database=" + MyArr(0).ToString() + " ;uid=" + MyArr(4).ToString() + "; pwd=" + MyArr(5).ToString() + ";"
            sConnSAP.ConnectionString = sCon
            sConnSAP.Open()

            Using sConnSAP
                Dim MyCommand As SqlCommand = New SqlCommand(QueryString, sConnSAP)
                MyCommand.CommandType = CommandType.Text
                Dim da As SqlDataAdapter = New SqlDataAdapter()
                Dim mytable As DataSet = New DataSet()
                da.SelectCommand = MyCommand
                da.SelectCommand.CommandTimeout = 120
                da.Fill(mytable)
                sConnSAP.Close()
                Return mytable
            End Using
        Catch ex As Exception
            Return ReturnMessage("-1", ex.ToString)
        End Try
    End Function
    Public Shared Sub WriteLog(ByVal Str As String)
        Dim oWrite As IO.StreamWriter
        Dim FilePath As String
        FilePath = "C:\DMSO\SBOWEB.txt"

        If IO.File.Exists(FilePath) Then
            oWrite = IO.File.AppendText(FilePath)
        Else
            oWrite = IO.File.CreateText(FilePath)
        End If
        oWrite.Write(Now.ToString() + ":" + Str + vbCrLf)
        oWrite.Close()
    End Sub
    Public Function ReturnMessage(ErrCode As Integer, ErrMsg As String) As DataSet
        Dim connect As New Connection()
        Dim ds As New DataSet

        Dim dt As New DataTable("ReturnMessage")
        dt.Columns.Add("ErrCode")
        dt.Columns.Add("ErrMsg")

        Dim drNew As DataRow = dt.NewRow
        drNew("ErrCode") = ErrCode
        drNew("ErrMsg") = ErrMsg

        dt.Rows.Add(drNew)

        ds.Tables.Add(dt.Copy)
        Return ds
    End Function
    Public Function ObjectGetAll_Query_SAP_JSON(ByVal QueryString As String) As String
        Try
            Dim ds As DataSet = ObjectGetAll_Query_SAP(QueryString)
            If ds.Tables.Count > 0 Then
                Return clXml2Ds.DataTableToJSON(ds.Tables(0))
            Else
                Return "ERROR - QUERY RETURN NO TABLE"
            End If

        Catch ex As Exception
            Return ex.ToString
        End Try
    End Function
End Class
